package com.future.springboot_swing;

import com.future.springboot_swing.page.MainPage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;

import javax.swing.*;

@SpringBootApplication
public class SpringbootSwingApplication {

    private static ApplicationContext APPLICATION_CONTEXT = null;

    public static void main(String[] args) {
        APPLICATION_CONTEXT = new SpringApplicationBuilder(SpringbootSwingApplication.class)
                .headless(false)
                .run(args);
        SwingUtilities.invokeLater(() -> createAndShowGUI());
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Business Tools");
        //主页面
        frame.setContentPane(((MainPage)APPLICATION_CONTEXT.getBean("mainPage")).getRootComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(frame, 1000, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    /**
     * 设置窗口的大小,无论使用怎样的windows主题,都能灵活的应对,显示合适的窗口大小,一定要在JFrame.setVisible(true)之前调用,
     * @author lcao
     * @date 2020/4/12 15:54
     * @param frame 窗体
     * @param width 宽
     * @param height 高
     **/
    public static void setSize(JFrame frame, int width, int height) {
        //一定要在JFrame显示之前调用
        frame.pack();
        //在默认的setResizable(true); 情况下,titleDefaultHeight = 27,但如果 setResizable(false); titleDefaultHeight = 25
        int titleDefaultHeight = frame.isResizable() ? 27 : 25; //默认开发阶段使用windows 经典主题,标题栏高度= 27
        int containerHeight = frame.getContentPane().getHeight();
        int frameHeight = frame.getHeight();
        int titleHeight = frameHeight - containerHeight; //这里就可以得到当前标题栏的实际高度
        if (titleHeight > titleDefaultHeight) {
            int h = titleHeight - titleDefaultHeight; //得到当前使用的主题,标题栏 - 经典主题下的标题栏高度 = 差值
            frame.setSize(width, height + h); //最终确定窗口的高度
        } else {
            frame.setSize(width, height);
        }
    }

}
